#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_producer(new ProducerThread(this))
    , m_consumer(new ConsumerThread(this))
{
    ui->setupUi(this);
    
    // 连接信号槽
    connect(ui->stopButton, &QPushButton::clicked,  // 新增
            this, &MainWindow::close);
    
    connect(m_producer, &ProducerThread::dataProduced,
            m_consumer, &ConsumerThread::handleData);
    
    connect(m_consumer, &ConsumerThread::resultReady,
            this, &MainWindow::onResultReady);
    
    m_consumer->start();
    m_producer->start();
}

MainWindow::~MainWindow()
{
    m_producer->stop();
    m_producer->wait();
    
    m_consumer->quit();
    m_consumer->wait();
    
    delete ui;
}

void MainWindow::onResultReady(QString result)
{
    ui->textEdit->append(result);  // 在UI显示处理结果
}

// 新增按钮槽函数实现
void MainWindow::on_stopButton_clicked()
{
    close();  // 关闭窗口会触发线程清理
}
